﻿using System;
using Equation.Enterprise.Framework.Domain.Model;
using System.Collections.Generic;

namespace ENail.Domain.Core
{
    public class Receipt : Entity
    {
        public virtual Registration Registration { get; set; }
        public virtual IList<ReceiptItem> ReceiptItems { get; set; }
        public double SubTotal { get; set; }
        public double Discount { get; set; }
        public double Tax { get; set; }
        public double Total { get; set; }
        
        public bool IsActive { get; set; }

        public Receipt()
        {
            ReceiptItems = new List<ReceiptItem>();
            IsActive = true;
        }

        public void updateReceipt()
        {
            if (ReceiptItems.Count==0)  updateReceiptItems();
            updateSubTotal();
            updateTotal();
        }

        private void updateReceiptItems()
        {
            ReceiptItems.Clear();
            foreach (var assignment in Registration.Assignments)
                ReceiptItems.Add(new ReceiptItem
                                 {
                                     Receipt = this,
                                     Assignment = assignment,
                                     Price = assignment.NailService.Price
                                 });
        }

        private void updateSubTotal()
        {
            SubTotal = 0;
            foreach (var item in ReceiptItems)
                SubTotal += item.Price;
        }

        private void updateTotal()
        {
            Total = (SubTotal - Discount) * (1 + Tax);
        }
    }
}
